<template>
  <div>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="small">
        <el-form-item label="装具名称">
          <el-input v-model="queryParam.deviceName" placeholder="装具名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="装具类型">
          <el-select v-model="queryParam.deviceType" placeholder="请选择装具类型" class="full-width-input" clearable>
            <el-option v-for="(item, index) in deviceTypeOptions" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">添加</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <SortableTable :table-props="tableProps" @update="UpdateTable"> </SortableTable>
      <el-table :data="tableData" border style="width: 100%" height="50vh">
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
        </el-table-column>
        <el-table-column label="操作" min-width="180">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-edit" link size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm class="button10Left" title="确定要删除吗？" @confirm="() => handleDelete(row)">
              <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <cabinet-dialog ref="cabinetDialog" @ok="loadData"></cabinet-dialog>
  </div>
</template>

<script>
import { pageList, del } from '@/api/bas/cabinet'
import { getDictItems } from '@/api/sys/dict'
import cabinetDialog from '@/views/Setting/cabinet/cabinetDialog.vue'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Cabinet',
  components: { cabinetDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      deviceTypeOptions: [],
      tableData: [],
      tableProps: [
        { title: '装具名称', prop: 'deviceName', width: '', checked: true },
        { title: '所在库房', prop: 'repositoryName', width: '', checked: true },
        { title: '区号', prop: 'quNo', width: '', checked: true },
        { title: '装具IP', prop: 'deviceIp', width: '', checked: true },
        { title: '装具端口', prop: 'devicePort', width: '', checked: true },
      ],
    }
  },

  mounted() {
    this.getBrandType()
    this.loadData()
  },

  methods: {
    onSubmit() {
      this.loadData()
    },
    getBrandType() {
      getDictItems('brand_type').then((res) => {
        if (res.data.code === 200) {
          this.deviceTypeOptions = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    loadData() {
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    UpdateTable(tableProps) {
      this.tableProps = this.onUpdateTable(tableProps)
    },
    // 打开对话框
    handleAdd() {
      this.$refs.cabinetDialog.add()
    },
    handleEdit(row) {
      this.$refs.cabinetDialog.edit(row)
    },
    handleDelete(row) {
      del(row.deviceId)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
      // this.tableData = this.tableData.filter(it => it.id !== row.id)
    },
  },
}
</script>

<style lang="less" scoped></style>
